package _07_串;

public class _434_字符串中的单词数 {

    public static void main(String[] args) {
        _434_字符串中的单词数 v = new _434_字符串中的单词数();
        String str = "Hello, my name is John";
        System.out.println(v.countSegments(str));
    }

    public int countSegments(String s) {
        char[] chars = s.toCharArray();
        boolean isLetter = false;
        int count = 0;
        for (char aChar : chars) {
            if (aChar != ' ') {
                // 不是空格
                if (!isLetter) {
                    count++;
                    isLetter = true;
                }
            } else {
                isLetter = false;
            }
        }
        return count;
    }

}
